package com.salim.leetcode.$166;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FractionToRecurringDecimal {
    public String fractionToDecimal(int numerator, int denominator) {
        if(numerator==0){
            return "0";
        }

        List<Long> result = new ArrayList<>();
        HashMap<String,Integer> repeat = new HashMap<>();
        boolean negative = false;
        if(numerator<0 && denominator<0){
            negative = false;
        }else if(numerator<0||denominator<0){
            negative = true;
        }
        int pointIndex = 0;
        boolean pointed = false;
        String repeatKey = null;
        boolean repeated = false;

        String numeratorStr = String.valueOf(numerator);
        if(numeratorStr.charAt(0)=='-'){
            numeratorStr = numeratorStr.substring(1);
        }
        int strIndex = 0;
        long current = numeratorStr.charAt(strIndex)-48;

        while (current!=0||strIndex<numeratorStr.length()){
            //这里本来是int 由于-1/-2147483648超长改成了long
            long quotient = current/denominator;
            long remainder = current%denominator;

            //数字用完了之后才会出现重复
            if(strIndex>=numeratorStr.length() && repeat.containsKey(remainder+"|"+quotient)&& remainder!=0){
                repeatKey = remainder+"|"+quotient;
                repeated = true;
                result.add(quotient);
                break;
            }

            result.add(quotient);
            repeat.put(remainder+"|"+quotient,result.size()-1);

            if(!pointed){
                if(strIndex<numeratorStr.length()-1){
                    pointIndex++;
                } else if(!negative && remainder/denominator>0 || negative&&remainder/denominator<0){
                    pointIndex++;
                } else{
                    pointed = true;
                }
            }

            current = remainder*10;
            if(strIndex<numeratorStr.length()-1){
                current= current+numeratorStr.charAt(strIndex+1)-48;
            }
            strIndex++;
        }

        StringBuilder sb = new StringBuilder();
        if(negative){
            sb.append("-");
        }
        boolean trimZero = false;
        for(int i=0;i<result.size();i++){
            if(result.get(i)!=0){
                trimZero = true;
            }

            //针对长除法前面多余的0问题 需要整体移位
            if(result.get(i)==0 && pointIndex>i && !trimZero){
                continue;
            }

            sb.append(Math.abs(result.get(i)));
            if(pointed && pointIndex==i && i!=result.size()-1){
                sb.append(".");
            }
            if(repeated && !repeatKey.isEmpty() && repeat.get(repeatKey)==i+1){
                sb.append("(");
                for(Long temp:result.subList(repeat.get(repeatKey),result.size()-1)){
                    sb.append(Math.abs(temp));
                }
                sb.append(")");
                break;
            }
        }
        return sb.toString();
    }

    public static void main(String[] args){
        FractionToRecurringDecimal fractionToRecurringDecimal = new FractionToRecurringDecimal();
        System.out.println(fractionToRecurringDecimal.fractionToDecimal(1,2));
        System.out.println(fractionToRecurringDecimal.fractionToDecimal(2,-1));
        System.out.println(fractionToRecurringDecimal.fractionToDecimal(2,-3));
        System.out.println(fractionToRecurringDecimal.fractionToDecimal(333,4));
        System.out.println(fractionToRecurringDecimal.fractionToDecimal(1,6));
        System.out.println(fractionToRecurringDecimal.fractionToDecimal(22,7));
        System.out.println(fractionToRecurringDecimal.fractionToDecimal(500,10));
        System.out.println(fractionToRecurringDecimal.fractionToDecimal(-1, -2147483648));
        System.out.println(fractionToRecurringDecimal.fractionToDecimal(2147483647, 1));
        System.out.println(fractionToRecurringDecimal.fractionToDecimal(-2147483648, -10));
    }
}
